import os
from flask import Flask
from applications.common.script import init_script
from applications.config import BaseConfig
from applications.extensions import init_plugs
from applications.view import init_bps
from datetime import datetime
# @app.template_filter('datetimeformat')
def datetimeformat(value, format="%Y-%m-%d %H:%M"):
    """
    json datetime auto format like this : '2025-02-08T20:07:41'
    convert string to datetime and then format it.
    """
    if value is None:
        return ""
    dt = datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
    return dt.strftime(format)

def create_app():
    app = Flask(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
    # 引入配置
    app.config.from_object(BaseConfig)
    app.add_template_filter(datetimeformat, "datetimeformat")

    # 注册flask组件
    init_plugs(app)

    # 注册蓝图
    init_bps(app)
    print(app.url_map)

    # 注册命令
    init_script(app)

    return app
